package com.lw.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.lw.vod.service.VodService;
import com.lw.vod.utils.ConstantPropertiesUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class VodServiceImpl implements VodService {
    //上传视频
    @Override
    public String uploadVideo(MultipartFile file) {
        try {
            String originalFilename = file.getOriginalFilename();
            String title=originalFilename.substring( 0,originalFilename.lastIndexOf( "." ) );
            UploadStreamRequest request = new UploadStreamRequest( ConstantPropertiesUtil.KEY_ID, ConstantPropertiesUtil.KEY_SECRET, title, originalFilename, file.getInputStream());
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            if (response.isSuccess()) {
                return response.getVideoId();
            } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                return response.getVideoId();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }
}
